package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.common.Result;
import com.tan.steamship.domain.CollectPo;
import com.tan.steamship.domain.RoutesPo;
import com.tan.steamship.mapper.CollectMapper;
import com.tan.steamship.service.CollectService;
import com.tan.steamship.service.RoutesService;
import com.tan.steamship.vo.RoutesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 85726
* @description 针对表【collect】的数据库操作Service实现
* @createDate 2025-02-11 23:50:25
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, CollectPo>
    implements CollectService{
    @Autowired
    RoutesService routesService;

    @Override
    public List<RoutesVo> getAllCollect(Integer userId) {
        List<RoutesVo> routesVoList = new ArrayList<>();
        LambdaQueryWrapper<CollectPo> collectPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectPoLambdaQueryWrapper.eq(CollectPo::getUserId, userId);
        List<CollectPo> collectPos = this.list(collectPoLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(collectPos)) {
            List<Integer> routeIds = collectPos.stream().map(CollectPo::getRouteId).collect(Collectors.toList());
            List<RoutesPo> routesPoList = routesService.listByIds(routeIds);
            if (CollUtil.isNotEmpty(routesPoList)) {
                routesVoList = BeanUtil.copyToList(routesPoList, RoutesVo.class, new CopyOptions());
            }
        }
        return routesVoList;
    }

    @Override
    public Result addCollect(Integer routeId, Integer userId) {
        Result result = new Result();
        LambdaQueryWrapper<CollectPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectPo::getRouteId, routeId);
        queryWrapper.eq(CollectPo::getUserId, userId);
        queryWrapper.last("limit 1");
        CollectPo one = this.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(one)) {
            result.setMessage("该路线已收藏");
            result.setCode(201);
            return result;
        }
        CollectPo collectPo = new CollectPo();
        collectPo.setRouteId(routeId);
        collectPo.setUserId(userId);
        collectPo.setCollectTime(new Date());
        this.save(collectPo);
        result.setMessage("收藏成功");
        result.setCode(200);
        return result;
    }

    @Override
    public void deleteCollect(Integer routeId, Integer userId) {
        LambdaUpdateWrapper<CollectPo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CollectPo::getRouteId, routeId);
        updateWrapper.eq(CollectPo::getUserId, userId);
        this.remove(updateWrapper);
    }
}




